package procsim;

import java.awt.*;

public class PaintCPU extends PaintTab {
    public PaintCPU(Paint parent) {
        super(parent);
        // Coordinates
        Design.ABUS.addCoords(this, 50, 20, 630, 20);
        Design.ABUS.addCoords(this, 120, 21, 120, 139);
        Design.ABUS.addCoords(this, 630, 21, 630, 139);
        Design.DBUS.addCoords(this, 50, 50, 560, 50);
        Design.DBUS.addCoords(this, 150, 51, 150, 139);
        Design.DBUS.addCoords(this, 560, 51, 560, 139);
        Design.RDBUS.addCoords(this, 50, 80, 440, 80);
        Design.RDBUS.addCoords(this, 180, 81, 180, 139);
        Design.RDBUS.addCoords(this, 440, 80, 440, 200);
        Design.RDBUS.addCoords(this, 440, 200, 500, 200);
        Design.WRBUS.addCoords(this, 50, 100, 420, 100);
        Design.WRBUS.addCoords(this, 210, 101, 210, 139);
        Design.WRBUS.addCoords(this, 420, 100, 420, 250);
        Design.WRBUS.addCoords(this, 420, 250, 500, 250);
        Design.BUSYBUS.addCoords(this, 50, 120, 310, 120);
        Design.BUSYBUS.addCoords(this, 240, 121, 240, 139);

        // Collections
        elements.add(Design.CPU);
        elements.add(Design.OperUnit);
        elements.add(Design.ContrUnit);
        elements.add(Design.BlockMemory);
        elements.add(Design.BlockRegs);
        elements.add(Design.BlockInterface);
        elements.add(Design.BlockOper);
        elements.add(Design.BlockInterrupt);

        lines.add(Design.DBUS);
        lines.add(Design.ABUS);
        lines.add(Design.RDBUS);
        lines.add(Design.WRBUS);
        lines.add(Design.BUSYBUS);

        addMouseListener(Design.ContrUnit);
        addMouseListener(Design.BlockMemory);
        addMouseListener(Design.BlockRegs);
        addMouseListener(Design.BlockInterface);
        addMouseListener(Design.BlockOper);
        addMouseListener(Design.BlockInterrupt);
    }
    @Override
    public void paint(Graphics g){
        super.paint(g);
        Graphics2D gnew = (Graphics2D)g;
        Util.saveGraphics(gnew);
        gnew.setColor(Color.GRAY);
        gnew.setStroke(new BasicStroke(2));
        
        // ABUS
        drawArrow(gnew, 120, 21, 0);
        drawArrow(gnew, 630, 139, 6);
        // DBUS
        drawArrow(gnew, 150, 51, 0);
        drawArrow(gnew, 150, 139, 6);
        drawArrow(gnew, 560, 51, 0);
        drawArrow(gnew, 560, 139, 6);
        gnew.setStroke(new BasicStroke(1));
        // RDBUS
        drawArrow(gnew, 180, 81, 0);
        drawArrow(gnew, 499, 200, 3);
        // WRBUS
        drawArrow(gnew, 210, 101, 0);
        drawArrow(gnew, 499, 250, 3);
        // BUSY
        drawArrow(gnew, 240, 121, 0);
        drawArrow(gnew, 240, 139, 6);

        // grouping signals
        gnew.drawLine(40, 15, 42, 15); // -
        gnew.drawLine(40, 15, 40, 69); // |
        gnew.drawLine(40, 68, 38, 70); // /
        gnew.drawLine(40, 72, 38, 70); // \
        gnew.drawLine(40, 72, 40, 125); // |
        gnew.drawLine(40, 125, 42, 125); // -
        gnew.drawString("bus", 13, 73);

        Util.restoreGraphics(gnew);
    }
    private void drawArrow(Graphics2D g, int x, int y, int direction){ // direction kao na satu :)
        if(direction==0 || direction==12){
            g.drawLine(x, y, x-3, y+6);
            g.drawLine(x, y, x+3, y+6);
        } else if(direction==3){
            g.drawLine(x, y, x-6, y-3);
            g.drawLine(x, y, x-6, y+3);
        } else if(direction==6){
            g.drawLine(x, y, x-3, y-6);
            g.drawLine(x, y, x+3, y-6);
        } else if(direction==9){
            g.drawLine(x, y, x+6, y-3);
            g.drawLine(x, y, x+6, y+3);
        }
    }
}
